package com.example.dish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dish.mapper.SetmealMapper;
import com.example.dish.service.CategoryService;
import com.example.dish.service.SetmealDishService;
import com.example.dish.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dish_model.dto.SetmealDTO;
import com.example.dish_model.entity.Category;
import com.example.dish_model.entity.Setmeal;
import com.example.dish_model.entity.SetmealDish;
import com.example.exceptions.BusinessException;
import com.example.exceptions.enumetation.ResponseEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author sjz
 * @since 2024-06-30
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {


    @Resource
    private SetmealDishService setmealDishService;

    @Resource
    @Lazy
    private CategoryService categoryService;

    /**
     * 新增套餐,操作tb_setmeal和tb_setmeal_dish表
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveSetmealWithDish(SetmealDTO setmealDTO) {
        log.info("setmealDTO:{}", setmealDTO.toString());
        //保存基本字段
        this.save(setmealDTO);

        //从DTO中获取对应的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //保存对应菜品信息
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDTO.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void updateStatus(int status, List<Setmeal> setmealList) {

        //判断当前点击的是停售按钮还是启售按钮
        if (status == 0) {
            //当前点击的是停售按钮
            for (Setmeal setmeal : setmealList) {
                //修改状态为停售
                setmeal.setStatus(0);
                this.updateById(setmeal);
            }
        } else {
            //当前点击的是启售按钮
            for (Setmeal setmeal : setmealList) {
                //修改状态为启售
                setmeal.setStatus(1);
                this.updateById(setmeal);
            }
        }

    }

    @Override
    public List<Setmeal> getByIds(List<String> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        List<Setmeal> setmealList = this.list(wrapper);

        return setmealList;
    }

    @Override
    public SetmealDTO getSetmealWithDishById(String id) {
        //查setmeal获取普通字段
        Setmeal setmeal = this.getById(id);

        SetmealDTO setmealDTO = new SetmealDTO();
        //拷贝
        BeanUtils.copyProperties(setmeal, setmealDTO);

        //查setmeal_dish获取关联菜品字段
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        //赋值
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }


    /**
     * 修改套餐,操作tb_setmeal和tb_setmeal_dish表
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmealWithDish(SetmealDTO setmealDTO) {
        //更新tb_setmeal表基本信息
        this.updateById(setmealDTO);

        //删除对应的菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDTO.getId());
        setmealDishService.remove(wrapper);

        //重新添加对应的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDTO.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public String removeSetmealWihtDish(List<String> ids) {
        //查询套餐状态,判断是否允许删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        long count = this.count(queryWrapper);
        if (count > 0) {
            //如果不允许删除,抛出业务异常
            throw new BusinessException(ResponseEnum.SETMEAL_DELETE_FAIL);
        }
        //允许删除,先删除套餐表基本信息
        this.removeByIds(ids);
        //删除对应的菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(wrapper);

        return "套餐删除成功";

    }

    @Override
    public Page<SetmealDTO> getPage(long page, long pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //因为setmeal里面没有categoryName属性,需要DTO
        Page<SetmealDTO> setmealDTOPage = new Page<>();

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Setmeal::getName, name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行查询
        this.page(pageInfo, wrapper);
        //把pageInfo拷贝到setmealDTOPage中
        BeanUtils.copyProperties(pageInfo, setmealDTOPage, "records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDTO> list = records.stream().map((item) -> {
            SetmealDTO setmealDTO = new SetmealDTO();
            //拷贝基本字段
            BeanUtils.copyProperties(item, setmealDTO);
            //分类id
            String categoryId = item.getCategoryId();
            //查询获取分类名称
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            setmealDTO.setCategoryName(categoryName);
            return setmealDTO;
        }).collect(Collectors.toList());

        setmealDTOPage.setRecords(list);
        return setmealDTOPage;
    }
}
